package com.bsy.videostation.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import coil.load
import coil.transform.RoundedCornersTransformation
import com.bsy.framework.ext.onClick
import com.bsy.framework.manager.ActivityManager
import com.bsy.framework.utils.dpToPx
import com.bsy.videostation.adapter.lisener.OnItemClickListener
import com.bsy.videostation.model.HomeEntity
import com.bsy.videostation.model.VideoEntity
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.dg.videostation.R
import me.jessyan.autosize.AutoSize

/**
 * Created by since on 2024/5/28.
 * Copyright
 * 描述：
 */
class HomeMainAdapter : BaseQuickAdapter<HomeEntity, BaseViewHolder>(R.layout.item_home_main) {
    private var listener: OnItemClickListener<VideoEntity>? = null
    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        val emptyView = LayoutInflater.from(context).inflate(R.layout.list_empty, recyclerView, false)
        setEmptyView(emptyView)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
        AutoSize.autoConvertDensity(ActivityManager.top(), 768F, false)
        return super.onCreateViewHolder(parent, viewType)
    }

    override fun convert(holder: BaseViewHolder, item: HomeEntity) {
        item.vItem.forEachIndexed { index, videoEntity ->
            when (index) {
                0 -> bindItem(holder, videoEntity, R.id.iv1, R.id.iv1_app, R.id.tv1_title, R.id.cl1)
                1 -> bindItem(holder, videoEntity, R.id.iv2, R.id.iv2_app, R.id.tv2_title, R.id.cl2)
                2 -> bindItem(holder, videoEntity, R.id.iv3, R.id.iv3_app, R.id.tv3_title, R.id.cl3)
                3 -> bindItem(holder, videoEntity, R.id.iv4, R.id.iv4_app, R.id.tv4_title, R.id.cl4)
                4 -> bindItem(holder, videoEntity, R.id.iv5, R.id.iv5_app, R.id.tv5_title, R.id.cl5)
            }
        }
//        item.hItem.forEachIndexed { index, videoEntity ->
//            when (index) {
//                0 -> bindItem(holder, videoEntity, R.id.iv4, R.id.iv4_app, R.id.tv4_title)
//                1 -> bindItem(holder, videoEntity, R.id.iv5, R.id.iv5_app, R.id.tv5_title)
//            }
//        }
    }

    fun setCustomOnItemClickListener(listener: OnItemClickListener<VideoEntity>) {
        this.listener = listener
    }

    private fun bindItem(holder: BaseViewHolder, videoEntity: VideoEntity, imageViewId: Int, appIconId: Int, titleId: Int, parentId: Int) {
        holder.getView<ImageView>(imageViewId).load(videoEntity.cover) {
            error(R.drawable.no_img)
            transformations(RoundedCornersTransformation(dpToPx(10F)))
        }
        holder.getView<ImageView>(appIconId).load(videoEntity.sourceIcon) {
            transformations(RoundedCornersTransformation(dpToPx(10F)))
        }
//        holder.setVisible(titleId, videoEntity.isShowTitle)
        holder.setText(titleId, videoEntity.title)
        holder.getView<ConstraintLayout>(parentId).onClick(500) {
            listener?.onClick(videoEntity.index, videoEntity)
        }
    }
}